package com.mall.service;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import com.mall.ibatis.bean.Function;
import com.mall.ibatis.bean.FunctionMethod;
import com.mall.ibatis.bean.ICell;
import com.mall.ibatis.bean.JQGrid;
import com.mall.ibatis.bean.Method;
import com.mall.ibatis.bean.ReturnMessage;
import com.mall.ibatis.bean.Role;
import com.mall.ibatis.persistence.DaoConfig;
import com.mall.ibatis.persistence.iface.RoleDAO;
import com.ibatis.dao.client.DaoManager;

public class RoleService
{
	private static final RoleService instance = new RoleService();
	
	private DaoManager daoManager = DaoConfig.getDaomanager();
	
	private RoleDAO roleDAO = null;
	
	private RoleService()
	{
		roleDAO  = (RoleDAO) daoManager.getDao(RoleDAO.class);
	}
	
	public static RoleService getInstance()
	{
		return instance;
	}
	
	
	public List<Role> selectAllRole() throws Exception
	{
		return roleDAO.selectAllRole();
	}
	
	
	public Role selectRole(Role role)  throws Exception
	{
		return roleDAO.selectRole(role);
	}
	
	
	public void insertRole(Role role) throws Exception
	{
		roleDAO.insertRole( role);
	}
	
	public void updateRole(Role role) throws Exception
	{
		roleDAO.updateRole( role);
	}
	
	public void deleteRole(Role role) throws Exception
	{
		roleDAO.deleteRole(role);
	}
	
	public JQGrid getRoleList(List<Role> roleList,int page,int total) throws Exception
	{
		JQGrid jqGrid = new JQGrid();
		
		jqGrid.setPage(page);
		jqGrid.setRecord(roleList.size());
		jqGrid.setTotal(total);
		
		List<ICell> rowsList = jqGrid.getRowXML();
		
		
		for( Role role :roleList)
		{
			rowsList.add(role);
		}
		
		
		return jqGrid;
	}
	
	
	public List<Role> selectRolesByOperatorID(String operatorID) throws Exception
	{
		return roleDAO.selectRolesByOperatorID(operatorID);
	}
	
	public void updateOperatorRole(String operatorID, String roleIDs)
	throws Exception
	{
		roleDAO.updateOperatorRole(operatorID, roleIDs);
	}

	//ryh
	//向服务器请求勾选了哪些功能方法并返回给修改树状窗口
	public String methodIsCheck(String id) {
		//勾选的方法请求
		List<FunctionMethod> fms = roleDAO.methodIsCheck(id);
		//勾选的功能请求
		List<String> functions = roleDAO.functionIsCheck(id);
		StringBuffer bf = new StringBuffer();
		if(fms.size() > 0 || functions.size() > 0){
			bf.append("<str>");
			bf.append("<functionId>");
			for(String s : functions){
				bf.append(s + ",");
			}
			bf.append("</functionId>");
			bf.append("<methodId>");
			for(FunctionMethod fm : fms){
				bf.append(fm.getFunctionId() + fm.getMethodId() + ",");//组合id形成唯一
			}
			bf.append("</methodId>");
			bf.append("</str>");
			return bf.toString();
		}else{
			return bf.append("<str>notData</str>").toString();
		}
		
	}
	
	//方法与功能的对应关系的保存
	public ReturnMessage addMethod(String str,String id,String functionStr) {	
		Set<String> set = new HashSet<String>();
		if(functionStr.trim().length() > 0){
			//把功能id的字符串组合分解成数组
			String[] functionStrs = functionStr.split(",");
			//用set集合来去掉重复
			for(String s : functionStrs){
				set.add(s);
			}
		}
		
		List<String[]> list = new ArrayList<String[]>();
		List<FunctionMethod> list2 = new ArrayList<FunctionMethod>();
		if(str.trim().length() > 0){
			//解析str字符串，并放入数组
			str = str.substring(1);
			String[] s = str.split(";");
			for(int i=0;i<s.length;i++){
				String[] s1 = s[i].split(",");
				list.add(s1);
			}
			
			//str字符串解析后放入对象集合
			for(String[] str1 : list){
				if(str1.length > 1){
					for(int i=1;i<str1.length;i++){
						FunctionMethod fm = new FunctionMethod();
						//每次为对象添加相应的functionId，每个功能对应增、删、改、
						fm.setRoleId(id);
						fm.setFunctionId(str1[0]);
						fm.setMethodId(str1[i]);
						list2.add(fm);
					}
				}
			}
		}
		daoManager.startTransaction();
		ReturnMessage message = null;
		try{
			//增加数据前先删除相关数据,按照功能的角色id号来删除
			roleDAO.deleteMethodFunction(id);
			//先删除已经有的功能
			roleDAO.deleteRoleFunction(id);
			//批量插入功能集合
			for(String functionId : set){
				roleDAO.addRoleFunction(id,functionId);
			}
			String str3 = null;
			//批量插入方法的id号到role_method表
			for(FunctionMethod fm : list2){
				str3 = roleDAO.addMethodFunction(fm);//循环插入批量数据
				if(str == null){
					return message = new ReturnMessage("0","添加失败");
				}
			}
			message = new ReturnMessage("1");
			
			daoManager.commitTransaction();
		}catch(Exception e){
			message = new ReturnMessage("0","添加失败" + e.getMessage());
		}finally{
			daoManager.endTransaction();
		}
		
		return message;
	}
	
	//查询出每个功能模块中有哪些方法
	public List<Method> selectAllMethod(){
		return roleDAO.selectAllMethod();
	}
	
	public List<Function> roleLook() {
		List<Function> list =  roleDAO.roleLook();
		return list;
	}
	
	//ryh






	
}